﻿using Microsoft.AspNetCore.Mvc;
using SIMS.Shared.DTO;
using SIMS.WebApi.Models;
using SIMS.WebApi.Services.Course;

namespace SIMS.WebApi.Controllers
{
    /// <summary>
    /// 课程控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class CourseController : ControllerBase
    {
        private readonly ILogger<CourseController> logger;

        private readonly ICourseAppService _service;

        public CourseController(ILogger<CourseController> logger, ICourseAppService service)
        {
            this.logger = logger;
            this._service = service;
        }

        [HttpGet]
        public IActionResult GetCourses( int pageNum, int pageSize, string? courseName=null, string? teacher=null) { 
            var data = _service.GetCourses(courseName,teacher,pageNum,pageSize);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 获取课程信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetCourse(int id)
        {
            var data = _service.GetCourse(id);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 新增课程
        /// </summary>
        /// <param name="course"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddCourse(CourseDTO course)
        {
            var data = _service.AddCourse(course);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 修改课程
        /// </summary>
        /// <param name="course"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateCourse(CourseDTO course)
        {
            var data = _service.UpdateCourse(course);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }

        /// <summary>
        /// 删除课程
        /// </summary>
        /// <param name="id"></param>
        [HttpDelete]
        public IActionResult DeleteCourse(int id)
        {
            var data = _service.DeleteCourse(id);

            var res = new ResultModel();
            res.code = 1;
            res.msg = "success";
            res.data = data;
            return Ok(res);
        }
    }
}
